package com.hzqy.web.ac.epg;


import com.hzqy.commons.utils.ExcelUtil;
import com.hzqy.commons.utils.ExcelUtil.MyEntry;
import com.hzqy.commons.utils.FileType;
import com.hzqy.service.epg.AcTicketNumService;
import com.hzqy.web.vo.epg.AcTicketNumVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/acTicket")
public class AcTicketNumAction {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	private static final String[] allowedFileType = new String[]{"png", "jpg", "gif","zip","jar","tar","docx","doc","xlsx","xls","flv","mp4","mp3","mpg","avi","mov","wmv"};
	@Resource
	private AcTicketNumService serviceAction;
	@RequestMapping("/acTicketSelectAll")
	@ResponseBody
	public Object acSignSelect(AcTicketNumVo as, int page, int rows){
		Map<String,Object> SignListMap = new HashMap<String, Object>();
		SignListMap= serviceAction.selectAc(as,page, rows);
		return SignListMap;
	}
	@RequestMapping("/acTicketExcel")
	@ResponseBody
	public void acTicketExcel(AcTicketNumVo infoVo, HttpSession session, int page, int rows, HttpServletResponse response) throws IOException {
		Map<String,Object> ListMap = new HashMap<String, Object>();
		ListMap = serviceAction.selectAc(infoVo,1,100000);
		List<AcTicketNumVo> data=(List<AcTicketNumVo>)ListMap.get("rows");
		List<MyEntry<String, String>> excelHeader=new ArrayList<MyEntry<String, String>>();
		excelHeader.add(new MyEntry<String,String>("f_at_password","券"));
		excelHeader.add(new MyEntry<String,String>("f_at_frequency","兑换次数"));
		excelHeader.add(new MyEntry<String,String>("f_at_voteNum","赠票数"));
		excelHeader.add(new MyEntry<String,String>("f_at_openid","微信账号"));
		excelHeader.add(new MyEntry<String,String>("f_at_serverid","iptv账号"));
		excelHeader.add(new MyEntry<String,String>("f_at_resolution","类型（高清/标清）"));
		excelHeader.add(new MyEntry<String,String>("f_at_cdate","存入时间"));
		excelHeader.add(new MyEntry<String,String>("f_at_udate","使用时间"));
		boolean flag= ExcelUtil.generateExcel(data, excelHeader, response);
	}
/*	@RequestMapping("/importTicket")
	@ResponseBody
	public Object insertBatchTicket(@RequestParam(value = "uploadify") MultipartFile file ,HttpServletResponse response) throws Exception {
		response.setContentType("text/html;charset=UTF-8");
		if (FileType.checkFileType(file.getInputStream(), allowedFileType)) {
			int successCount = 0;
			StringBuffer str = new StringBuffer();
			InputStream inputStream = null;
			try {
				inputStream = file.getInputStream();
				LOG.debug("批量导入Ticket开始");
				List<Map<String, Object>> list = ExcelUtil.parseExcel(file.getInputStream(), ExcelUtil.ALLOWED_EMPTY_MODE, new String[]{});
				LOG.debug("解析excel完成, 开始循环入库" + list.size());
				for (int i = 0; i < list.size(); i++) {
					AcTicketNumVo Ticket = new AcTicketNumVo();
					Map<String, Object> map = list.get(i);
					Ticket.setF_at_password(map.get("赠票卡号").toString());
					Ticket.setF_at_voteNum(map.get("赠票数量").toString());
					if (map.get("状态") != null) {
						Ticket.setF_at_frequency(String.valueOf(map.get("状态")));
					} else {
						Ticket.setF_at_frequency("1");
					}
					if (map.get("iptv账号") != null) {
						Ticket.setF_at_serverid(String.valueOf(map.get("iptv账号")));
					} else {
						Ticket.setF_at_serverid("0");
					}
					if (map.get("openid") != null) {
						Ticket.setF_at_openid(String.valueOf(map.get("openid")));
					} else {
						Ticket.setF_at_openid("0");
					}
					boolean success = false;
					Map<String, Object> resultMap = serviceAction.insertTicketNumService(Ticket);
					success = (Boolean) resultMap.get("success");
					successCount += (success ? 1 : 0);
				}
				LOG.debug("批量完成" + successCount);
				successCount = successCount;
				str.append("一共").append(list.size()).append("条数据").append("批量导入完成,：").append(successCount).append("条数据成功").append(list.size() - successCount).append("条失败");
			} catch (Exception e) {
				LOG.error("", e);
				//str.append("批量导入过程有异常!" +"成功条数"+successCount+ e.getMessage());
			} finally {
				if (inputStream != null) {
					try {
						inputStream.close();
					} catch (IOException e) {
					}
				}
			}
			return str.toString();
		} else {
			return "";
		}
	}*/

}
